package com.lemurian.bankdata.bean;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.lemurian.bankdata.bean.base.BaseDaoHibernate;
import com.lemurian.bankdata.lite.logger.BankDataLogger;
import com.lemurian.entity.ClientDocument;

@Repository
public class ClientDocumentDAO extends BaseDaoHibernate<ClientDocument>{
	
	private BankDataLogger log = BankDataLogger.getLogger(ClientDocumentDAO.class.getName());

	@Override
	public List<ClientDocument> findByCriteria(Order order, Criterion... criterion) {
		try {
			log.info("Get Overide");
			Criteria criteria = getSession().createCriteria(ClientDocument.class);
			criteria.createCriteria("documentType", "documentType", Criteria.INNER_JOIN);
			criteria.createCriteria("documentRemarks", "documentRemarks", Criteria.INNER_JOIN);
			criteria.setCacheable(true);
			for (Criterion c : criterion) {
				criteria.add(c);
			}
			if (order!=null) criteria.addOrder(order);
			List<ClientDocument> list = criteria.list();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public ClientDocument findByClientAndDocs(int clientId, int docId) {
		try {
			log.info("Get Overide");
			Criteria criteria = getSession().createCriteria(ClientDocument.class);
			criteria.createCriteria("documentType", "documentType", Criteria.INNER_JOIN);
			criteria.createCriteria("documentRemarks", "documentRemarks", Criteria.INNER_JOIN);
			criteria.add(Restrictions.eq("client.id", clientId));
			criteria.add(Restrictions.eq("documentType.id", docId));
			
			criteria.setCacheable(true);
			
			ClientDocument clientDocument = (ClientDocument) criteria.uniqueResult();
			
			return clientDocument;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}